/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.autoconfigure.jwt;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * TokenProperties<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Component
@ConfigurationProperties(prefix = "star.token")
public class TokenProperties implements Serializable {

  /**
   * 访问Token的名称<br>
   * 一般存放在请求头中
   */
  @Getter @Setter private String name = "token";

  /**
   * 刷新Token的名称<br>
   * 可以写入响应头或者Cookie中
   */
  @Getter @Setter private String refreshName = "refresh-" + TokenProperties.this.name;

  /**
   * Token值前缀，可以为空<br>
   * 例如：Bearer TokenValue，Bearer就是生成的Token值的前缀
   */
  @Getter @Setter private String prefix = "Bearer ";

  /**
   * Token 有效期<br>
   * 单位：毫秒，默认 2H<br>
   * 小于零则不设置过期时间
   */
  @Getter @Setter private long ttl = 2L * 60 * 60 * 1000;

  /**
   * 在距离Token过时还是有多久自动刷新Token<br>
   * 单位：毫秒，默认 10M
   */
  @Getter @Setter private long autoRefreshTime = 10 * 60 * 1000;

  /**
   * Token 生成密钥<br>
   * 且不能太短，否则会报错：<br>
   * <span style="color: red">The specified key byte array is 48 bits which is not secure enough for
   * any JWT HMAC-SHA algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys
   * used with HMAC-SHA algorithms MUST have a size >= 256 bits (the key size must be greater than
   * or equal to the hash output size). Consider using the Jwts.SIG.HS256.key() builder (or
   * HS384.key() or HS512.key()) to create a key guaranteed to be secure enough for your preferred
   * HMAC-SHA algorithm. See https://tools.ietf.org/html/rfc7518#section-3.2 for more
   * information.</span> <br>
   * 至少 32 位
   */
  @Getter @Setter private String secretKey = "8888-12345678-9012-34567890-8888";

  /**
   * Cookie过期时间<br>
   * 单位：秒，默认 2H<br>
   * 当Token保存在Cookie中时
   */
  @Getter @Setter private int cookieTtl = 2 * 60 * 60;

  /**
   * 是否支持将Token，默认不持久化<br>
   *
   * <ul>
   *   <li>安全级别较高的情况下需要持久化Token
   *   <li>但是性能会受到一点影响
   * </ul>
   */
  @Getter @Setter private boolean persistence = false;
}
